package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.PayChannelAccountServiceApi;
import com.thb.pay.api.payment.vo.req.PayChannelAccountPageReq;
import com.thb.pay.api.payment.vo.req.PayChannelAccountReq;
import com.thb.pay.api.payment.vo.resp.PayChannelAccountVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.PayChannelAccount;
import com.thb.pay.payment.service.PayChannelAccountService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 支付-通道-账户表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class PayChannelAccountController implements PayChannelAccountServiceApi {

    @Autowired
    private PayChannelAccountService payChannelAccountService;

    @Override
    public PageResult<PayChannelAccountVo> findPage(@RequestBody PayChannelAccountPageReq pageReq) {
        IPage<PayChannelAccount> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<PayChannelAccount> pageResult = payChannelAccountService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, PayChannelAccountVo.class);
    }

    @Override
    public List<PayChannelAccountVo> findAllList() {
        List<PayChannelAccountVo> resultDataList = Lists.newArrayList();
        List<PayChannelAccount> targetDbResultList = payChannelAccountService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            PayChannelAccountVo resultData = new PayChannelAccountVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<PayChannelAccountVo> findListByChannelId(@RequestParam("channelId") Long channelId) {
        List<PayChannelAccountVo> resultDataList = Lists.newArrayList();
        LambdaQueryWrapper<PayChannelAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayChannelAccount::getChannelId, channelId);
        List<PayChannelAccount> targetDbResultList = payChannelAccountService.selectList(wrapper);
        targetDbResultList.forEach(dbResult -> {
            PayChannelAccountVo resultData = new PayChannelAccountVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public void add(@RequestBody PayChannelAccountReq payChannelAccountReq) {

        LambdaQueryWrapper<PayChannelAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayChannelAccount::getChannelId, payChannelAccountReq.getPayPassageId());
        wrapper.eq(PayChannelAccount::getThirdMchNo, payChannelAccountReq.getPassageMchId());
        List<PayChannelAccount> targetDbResultList = payChannelAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("passage.account.exist");
        }
        PayChannelAccount payChannelAccount = new PayChannelAccount();
        BeanUtil.copyProperties(payChannelAccountReq, payChannelAccount);
        payChannelAccountService.insert(payChannelAccount);
    }

    @Override
    public void update(@RequestBody PayChannelAccountReq payChannelAccountReq) {
        PayChannelAccount payChannelAccount = payChannelAccountService.getById(payChannelAccountReq.getId());
        if (payChannelAccount == null) {
            throw RRException.build("passage.account.not.exist");
        }

        LambdaQueryWrapper<PayChannelAccount> wrapper = new LambdaQueryWrapper();
        wrapper.eq(PayChannelAccount::getChannelId, payChannelAccountReq.getPayPassageId());
        wrapper.eq(PayChannelAccount::getThirdMchNo, payChannelAccountReq.getPassageMchId());
        List<PayChannelAccount> targetDbResultList = payChannelAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            if (targetDbResultList.get(0).getId().compareTo(payChannelAccountReq.getId()) != 0) {
                throw RRException.build("passage.account.exist");
            }
        }
        PayChannelAccount targetPayChannelAccount = new PayChannelAccount();
        BeanUtil.copyProperties(payChannelAccountReq, targetPayChannelAccount);
        payChannelAccountService.updateById(targetPayChannelAccount);
    }

    @Override
    public PayChannelAccountVo getById(@RequestParam("id") Long id) {
        PayChannelAccount payChannelAccount = payChannelAccountService.selectById(id);
        if (payChannelAccount != null) {
            PayChannelAccountVo resultData = new PayChannelAccountVo();
            BeanUtil.copyProperties(payChannelAccount, resultData);
            return resultData;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<PayChannelAccount> wrapper = new LambdaQueryWrapper();
        wrapper.in(PayChannelAccount::getId, ids);
        List<PayChannelAccount> targetDbResultList = payChannelAccountService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            payChannelAccountService.updateBatchById(targetDbResultList);
        }
    }
}
